<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%coupon_list}}".
 *
 * @property integer $id
 * @property integer $user_id
 * @property string $name
 * @property string $desc
 * @property integer $starttime
 * @property integer $endtime
 * @property double $price
 * @property integer $type
 * @property integer $time
 * @property integer $is_deleted
 * @property double $t_price
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $created_id
 * @property integer $updated_id
 * @property integer $status
 */
class CouponList extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%coupon_list}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'type', 'time', 'is_deleted', 'created_at', 'updated_at', 'status','price'], 'integer'],
            [['name', 'desc', 'price', 'type'], 'required'],
            [[ 't_price'], 'number'],
            [['name', 'desc', 'starttime', 'endtime', 'created_user', 'updated_user',], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'name' => '名称',
            'desc' => '描述',
            'starttime' => '开始时间',
            'endtime' => '结束时间',
            'price' => '金额',
            'type' => '类型：1：日期截止类型 2:天数截止',
            'time' => '天数',
            'is_deleted' => '是否删除',
            't_price' => '门槛金额',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'created_user' => '创建者',
            'updated_user' => '更新者',
            'status' => '1未使用2已使用3已过期',
        ];
    }
    
    /**
        
     * author;sunjian
     * createtime:2016年1月27日15:48:02
     * getCoupons 获取可用优惠劵
     * params uid price
     * return array
     *      */
    static function getCoupons($uid,$price){
        $time=date('Y-m-d');
        return CouponList::find()->where("status=1 and user_id=$uid and t_price<=$price and starttime<='$time' and endtime>='$time'")->asArray()->all();
    }
    
    /**

     * author:sunjian
     * createtime：2016年1月29日14:15:40
     * checkcoupon 校验优惠劵并置状态
     * params couponId price
     * return bool
     *      */
    static function checkCoupon($couponId,$price){
        $coupon=  CouponList::findOne($couponId);
        //判断status状态
        if($coupon->status!=1){
           return false; 
        }
        //判断时间
        $time=date('Y-m-d');
        if($time<$coupon->starttime||$time>$coupon->endtime){
           return false; 
        }
        //判断金额
        if($price<$coupon->t_price){
           return false;  
        }
        //修改状态
        $coupon->status=2;
        $user=Yii::$app->user->identity;
        $coupon->updated_user=$user->truename;
        $coupon->updated_at=time();
        if($coupon->save()){
            return $coupon->price;
        }else{
            return false;
        }
    }
}
